'''
    FogBugz Abstraction to facilitate the posting and editing of new cases
    through interfaces that are not FogBugz. Oh and please, let the tests
    drive any changes to this class. If you don't a man will come to you
    in your sleep... I'm not saying what he'll do, just FYI.

    Wayne Witzel III
    Tower Hill Insurance Group
    Copyright 2008
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

import unittest

from fogbugz import Authentication
from fogbugz import Case

class TestFogBugz(unittest.TestCase):
    def setUp(self):
        self.credentials = ('WWitzel','Password')
        self.bad_credentials = ('xyz','xyz')
        
    def tearDown(self):
        pass
    
    def testFailedAuthentication(self):
        
        auth = Authentication(*self.bad_credentials)
        
        auth.logon()
        self.assertFalse(auth.valid)
        
    def testAuthentication(self):
        auth = Authentication(*self.credentials)
        
        auth.logon()
        self.assertTrue(auth.valid)
        
        auth.logoff()
        self.assertFalse(auth.valid)
    
    def testFileAttachCase(self):
        auth = Authentication(*self.credentials)
        
        c = Case(auth)
        c.sTitle = 'Test Case File Attach'
        c.ixProject = 'Notes'
        c.ixPersonAssignedTo = 'Wayne Witzel III'
        c.sComputer = 'Systems'
        c.sCustomerEmail = 'wwitzel@thig.com'
        c.ixMailbox = 1
        
        c.attachFile('./tests/external/test.txt')
        c.attachFile('./tests/external/test2.txt')
        c.attachFile('./tests/external/test3.jpg')
        
        c.save()
        
        self.assertFalse(c.error)
        
    def testNewCase(self):
        auth = Authentication(*self.credentials)
        
        c = Case(auth)
        c.sTitle = 'Test Case'
        c.ixProject = 'Notes'
        c.ixPersonAssignedTo = 'Wayne Witzel III'
        c.sComputer = 'Systems'
        c.sCustomerEmail = 'wwitzel@thig.com'
        c.ixMailbox = 1
        
        c.save()
        
        self.assertFalse(c.error)
        self.assertTrue(c.ixBug)
        
    def testEditCase(self):
        auth = Authentication(*self.credentials)
                
        c = Case(auth, 299)
        self.assertEquals(c.ixBug,'299')
        
        import time
        timestamp = '%.0f' % time.time()
        title = 'Test Case (%s)' % timestamp
        
        c.sTitle = title
        c.save()
        
        c = Case(auth, 299)
        self.assertEquals(c.sTitle,title)
        
        
